package com.hjb.springbootdemo.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxyHandler implements InvocationHandler {

    private Object target;//被代理的对象

    public MyProxyHandler(Object target) {
        this.target = target;
    }

    public Object getInstance(Object target) {
        this.target = target;
        Class clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开始联系朋友一起出去吃宵夜");
        Object result = method.invoke(target, args);
        System.out.println("宵夜吃完了，肚子好饱啊");
        return result;
    }
}
